<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MealCardOrder extends Model
{
    const CREATED_AT = 'insert_time';
    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'lexiang_card_order';

    protected $primaryKey = 'order_id';

    public $timestamps = false;
    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = ['order_num','user_id','card_id','order_price','order_status','insert_time','order_desc'];

    public function add($data)
    {
        $this->order_num = $this->createOrderNumber();
        $this->user_id = $data['user_id'];
        $this->card_id = $data['card_id'];
        $this->user_card_id = 0;
        $this->order_price = $data['order_price'];
        $this->order_status = $data['order_status'];
        $this->insert_time = $data['insert_time'];
        $this->order_desc = $data['order_desc'];
        if($this->save()){
            return [
                'success'=>true,
                'code'=>'000',
                'message'=>'生成订单',
                'data'=>[
                    'order_num'=>$this->order_num,
                ]
            ];
        }
    }

    /**
     * 生成订单号
     */
    public function createOrderNumber()
    {
        return time().rand(11111,99999);
    }

    public function orderCount($userid)
    {
    	return self::where('user_id',$userid)->where('order_status','1')->count();
    }

    public function getShareUserNameAttribute($v){
        return User::where('user_id',$this->share_userid)->value('nicename') ?? '无';
    }

    /**
     * 商圈
     */
    public function getSucircleNameAttribute($v)
    {
        return Circle::where('circle_id',$this->circle_range_id)->value('circle_name') ?? '未设置';
    }
}
